// TypeScript file
module Fighter {

    /**
     * 玩家漏兵防守部分---刘伟
     */
    export class Defense extends egret.DisplayObjectContainer {
        constructor() {
            super();
            this.addEventListener(egret.Event.ADDED_TO_STAGE, this.addToStage, this);
            this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.removeFromStage, this);
            this.Timer_fire.addEventListener(egret.TimerEvent.TIMER, this.fire, this);
            this.flyManager = new FlyManager(this);
            this.BulletManager = new Bullet.BulletManager(this);
            this.Animation = new Animation.AnimationManager(this);
        }
        /**防御场景是否可以判断结束 */
        public static isOver: boolean = false;
        /**防御场景是否结束*/
        public static defenseEnd:boolean;
        /**开始制造飞机 */
        public static startCreatPlane = false;
        /**飞机层 */
        public flyManager: FlyManager;
        /**子弹层 */
        public BulletManager: Bullet.BulletManager;

        /**容器名 */
        public name: string = "Defense";
        /**游戏防守结束，进入下一波 */
        public toNextTurn: boolean;
        /**游戏失败 */
        public is_game_over: boolean;

        /**单机下允许进入下一轮 */
        public static allowToNextTurn: boolean = false;

        /**发射子弹间隔*/
        private Timer_fire: egret.Timer = new egret.Timer(200, 0);
        /**上一帧时间点 */
        public lastTimer: number;
        /**动画管理类*/
        public Animation: Animation.AnimationManager;
        public addToStage() {
            Defense.defenseEnd=false;
            //在单机模式下，是否允许防御场景直接切换下一轮空战场景
            Defense.allowToNextTurn = false;
            //游戏是否进入下一轮的布尔值初始化
            this.toNextTurn = false;
            Defense.isOver = false;
            if (GameUtil.PlayerMessage.instance.onePerson) {
                this.flyManager.fightRoundStart(GameUtil.PlayerMessage.instance.Game2addEnemy);               
                Defense.isOver = true;
            }
            this.Timer_fire.start();

            //添加每帧监听
            this.addEventListener(egret.Event.ENTER_FRAME, this.Update, this);
        }

        private removeFromStage() {
            this.removeChildren();
        }

        /**每帧运行*/
        public Update() {
            //计算每帧间隔时间
            let now = egret.getTimer();
            let deltaTime = now - this.lastTimer;
            this.lastTimer = now;

            if (!GameUtil.PlayerMessage.instance.onePerson && Defense.startCreatPlane) {
                Defense.startCreatPlane = false;
                this.flyManager.fightRoundStart(GameUtil.PlayerMessage.instance.Game2addEnemy);
            }

            this.flyManager.update(deltaTime);
            this.BulletManager.update(deltaTime);
            this.batteryAngle();
            if (Defense.isOver) {
                this.defenseEnd();
            }
            this.isGameOver();

        }
        /**炮台的转向*/
        public batteryAngle() {
            if (this.flyManager.productflys.length > 0) {
                let plane = this.flyManager.productflys[0].fly;
                let angle =
                    Math.atan((plane.x - GameUtil.PlayerMessage.instance.HomePoint.x)
                        / (GameUtil.PlayerMessage.instance.HomePoint.y - plane.y));
                GameUtil.PlayerMessage.instance.HomeAngle = angle * 57.3;
            }
            else {
                GameUtil.PlayerMessage.instance.HomeAngle = 0;
            }
        }
        /**炮台发射子弹 */
        private fire() {
            let point = new egret.Point(GameUtil.PlayerMessage.instance.HomePoint.x, GameUtil.PlayerMessage.instance.HomePoint.y);
            this.BulletManager.createBullet(1, point, GameUtil.PlayerMessage.instance.HomeAngle / 57.3);
            //正在攻击
            GameUtil.PlayerMessage.instance.is_myTurryAtk = true;
        }

        /**判断场景结束 */
        private defenseEnd() {
            if (this.flyManager.productflys.length == 0 && !Defense.allowToNextTurn) {
                Defense.allowToNextTurn = true;
                this.Timer_fire.reset();
                
                this.BulletManager.fightRoundEnd();
                GameUtil.PlayerMessage.instance.is_myTurryAtk = false;
                this.removeEventListener(egret.Event.ENTER_FRAME, this.Update, this);
                this.toNextTurn = true;
                Defense.isOver = false;
                Defense.defenseEnd=true;
            }
        }

        /**判断游戏是否失败 */
        private isGameOver() {
            if (GameUtil.PlayerMessage.instance.enemyHomeHp <= 0) {
                for (let i = 0; i < this.flyManager.productflys.length; i++) {
                    this.flyManager.productflys[i].destroy();
                    this.flyManager.productflys.splice(i, 1);
                    i--;
                }
            }
            if (GameUtil.PlayerMessage.instance.BaseHp <= 0) {
                
                GameUtil.PlayerMessage.instance.isGameOver = true;
                this.Timer_fire.reset();
                for (let i = 0; i < this.flyManager.productflys.length; i++) {
                    this.flyManager.productflys[i].destroy();
                    this.flyManager.productflys.splice(i, 1);
                    i--;
                }
                this.removeEventListener(egret.Event.ENTER_FRAME, this.Update, this);
                this.is_game_over = true;
                GameUtil.PlayerMessage.instance.getEnemy = [];
                GameUtil.PlayerMessage.instance.sendEnemy = [];
                GameUtil.PlayerMessage.instance.Game2addEnemy = [];
                 Ai.AiBool = false;
                if (GameUtil.PlayerMessage.instance.onePerson) {
                    for (let i = 0; i < GameUtil.PlayerMessage.instance.PlaneNum; i++) {
                        GameUtil.PlayerMessage.instance.getEnemy.push(EnemyType["feiji0_png"]);
                    }
                   
                }
                this.removeChildren();
            }
        }



    }
}